<?php

/**
 * Created by PhpStorm.
 * User: lawrence
 * Date: 06.06.2014
 * Time: 20:49
 */
class db_model_find extends CI_Model
{
    function find_one($id)
    {
        $this->load->database();

        $dataQuery = $this->db->query('select * from roomtable where id="' . $id . '" LIMIT 1;');

        $row = $dataQuery->result_array();

	 $row = $row[0];

        $row['location'] = $this->convert_location($row['location']);
        $row['orientation'] = $this->convert_orientation($row['orientation']);
        $row['aircond'] = $this->convert_air_cond($row['aircond']);
        $row['tv'] = $this->convert_tv($row['tv']);
        $row['minibar'] = $this->convert_minibar($row['minibar']);

        return $row;
    }

    function find_rezerved($id, $startDate, $endDate)
    {
        $this->load->database();

        $dateQuery = $this->db->query('select roomId as id from rezerved where ' . $startDate . '>=startdate AND ' . $startDate . '<=enddate OR ' .
                                                                            $endDate . ' >=startdate AND ' . $endDate .'<= enddate OR ' .
                                                                            'startdate>=' . $startDate . ' AND startdate<=' . $endDate . ' OR ' .
                                                                            'enddate>=' . $startDate .' AND enddate<=' . $endDate . ' LIMIT 1;');

        if($dateQuery->num_rows() > 0)
        {
	     foreach($dateQuery->result_array() as $row)
	     {
                if($row['id'] == $id)
		  {
		       return true;
	 	  }
	     }
        }

        return false;
    }

    function find_all($location, $floor, $rooms, $geog, $air, $tv, $bar, $r1, $r2, $startDate, $endDate)
    {
        $this->load->database();

        $queryStr = 'select * from roomtable ';

        $queryCond = '';

        if ($location != 'Any') {
            switch ($location) {
                case 'Atena':
                    $location = '1000';
                    break;
                case 'Roma':
                    $location = '1001';
                    break;
                case 'Venetia':
                    $location = '1002';
                    break;
            }

            $queryCond = $queryCond . 'location=' . $location;
        }

        if ($floor != 'Any') {
            if ($queryCond != '') {
                $queryCond = $queryCond . ' AND floor=' . $floor;
            } else {
                $queryCond = $queryCond . ' floor=' . $floor;
            }
        }

        switch ($rooms) {
            case 'Any':
                break;
            case 'Fixed':

                if ($queryCond != '') {
                    $queryCond = $queryCond . ' AND roomcount=' . $r1;
                } else {
                    $queryCond = $queryCond . ' roomcount=' . $r1;
                }
                break;
            case 'Range':
                if ($queryCond != '') {
                    $queryCond = $queryCond . ' AND roomcount>=' . $r1 . ' AND roomcount <=' . $r2;
                } else {
                    $queryCond = $queryCond . ' roomcount>=' . $r1 . ' AND roomcount <=' . $r2;
                }
                break;
        }

        if ($geog != 'Any') {
            if ($queryCond != '') {
                $queryCond = $queryCond . ' AND orientation=\'' . $geog . '\'';
            } else {
                $queryCond = $queryCond . ' orientation=\'' . $geog . '\'';
            }
        }

        if ($queryCond != '') {
            $queryCond = $queryCond . ' AND aircond=' . $air;
        } else {
            $queryCond = $queryCond . ' aircond=' . $air;
        }

        if ($tv != 'Any') {
            if ($queryCond != '') {
                $queryCond = $queryCond . ' AND tv=\'' . $tv . '\'';
            } else {
                $queryCond = $queryCond . ' tv=\'' . $tv . '\'';
            }
        }

        if ($bar != 'Any') {
            if ($queryCond != '') {
                $queryCond = $queryCond . ' AND minibar=\'' . $bar . '\'';
            } else {
                $queryCond = $queryCond . ' minibar=\'' . $bar . '\'';
            }
        }

        $queryStr = $queryStr . ' where ' . $queryCond . ';';

        $this->db->order_by('id', 'ASC');

        $query = $this->db->query($queryStr);

        $dateQuery = $this->db->query('select * from rezerved where \'' . $startDate . '\'>=startdate AND \'' . $startDate . '\'<=enddate OR \'' .
                                                                          $endDate . '\' >=startdate AND \'' . $endDate .'\'<= enddate OR ' .
                                                                          'startdate>=\'' . $startDate . '\' AND startdate<=\'' . $endDate . '\' OR ' .
                                                                          'enddate>=\'' . $startDate .'\' AND enddate<=\'' . $endDate . '\';');

        $dateQueryResult = $dateQuery->result_array();

        if ($query->num_rows() > 0) {

            $data = array();

            foreach ($query->result_array() as $row) {
                if (!$this->check_room_interval($dateQueryResult, $row['id'])) {
                    $row['location'] = $this->convert_location($row['location']);
                    $row['orientation'] = $this->convert_orientation($row['orientation']);
                    $row['aircond'] = $this->convert_air_cond($row['aircond']);
                    $row['tv'] = $this->convert_tv($row['tv']);
                    $row['minibar'] = $this->convert_minibar($row['minibar']);

                    $data[] = $row;
                }
            }

            return $data;
        }
    }

    function find_user_specific($user)
    {
        $this->load->database();

        $query = $this->db->query('select * from rezerved where userid=' . $user . ';');

        $data = array();

        if($query->num_rows() > 0)
        {
            foreach($query->result_array() as $row)
            {
                $roomSpec = $this->db->query('select * from roomtable where id=' . $row['id'] . ' LIMIT 1;');

                if($roomSpec->num_rows() > 0)
                {
                    $roomSpec = $roomSpec->result_array();

                    $newrow['tabid'] = $row['id'];
                    $newrow['id'] = $row['roomid'];

                    $newrow['pool'] = $row['pool'] == '1' ? 'Yes' : 'No';
                    $newrow['jaccuzi'] = $row['jaccuzi'] == '1' ? 'Yes' : 'No';
                    $newrow['massage'] = $row['massage'] == '1' ? 'Yes' : 'No';
                    $newrow['haman'] = $row['haman'] == '1' ? 'Yes' : 'No';
                    $newrow['sauna'] = $row['sauna'] == '1' ? 'Yes' : 'No';
                    $newrow['salt'] = $row['salt'] == '1' ? 'Yes' : 'No';
                    $newrow['fitness'] = $row['fitness'] == '1' ? 'Yes' : 'No';
                    $newrow['steambath'] = $row['steambath'] == '1' ? 'Yes' : 'No';
                    $newrow['startdate'] = $row['startdate'];
                    $newrow['enddate'] = $row['enddate'];

                    $roomSpec = $roomSpec[0];

                    $newrow['location'] = $this->convert_location($roomSpec['location']);
                    $newrow['floor'] = $roomSpec['floor'];
                    $newrow['roomcount'] = $roomSpec['roomcount'];
                    $newrow['orientation'] = $this->convert_orientation($roomSpec['orientation']);
                    $newrow['aircond'] = $this->convert_air_cond($roomSpec['aircond']);
                    $newrow['tv'] = $this->convert_tv($roomSpec['tv']);
                    $newrow['minibar'] = $this->convert_minibar($roomSpec['minibar']);

                    $data[] = $newrow;
                }
            }
        }

        return $data;
    }

    function remove_rezervation($rezid)
    {
        $this->load->database();

        $this->db->query('delete from rezerved where id=' . $rezid .';');
    }

    function convert_location($location)
    {
        switch ($location) {
            case '1000':
                return 'Atena';
            case '1001':
                return 'Roma';
                break;
            case '1002':
                return 'Venetia';
                break;
        }
    }

    function convert_orientation($orientation)
    {
        switch ($orientation) {
            case 'north':
                return 'North';
            case 'south':
                return 'South';
            case 'east':
                return 'East';
            case 'west':
                return 'West';
            case 'north-east':
                return 'North - East';
            case 'north-west':
                return 'North - West';
            case 'south-east':
                return 'South - East';
            case 'south-west':
                return 'South - West';
        }
    }

    function convert_tv($tv)
    {
        switch ($tv) {
            case 'classic':
                return 'Classic';
            case 'led':
                return 'LED';
            case '3d':
                return '3D';
        }
    }

    function convert_minibar($bar)
    {
        switch ($bar) {
            case 'without':
                return 'Without';
            case 'minimal':
                return 'Minimal';
            case 'all-inclusive':
                return 'All Inclusive';
        }
    }

    function convert_air_cond($val)
    {
        return $val == 1 ? 'Yes' : 'No';
    }

    function check_room_interval($datainterval, $roomId)
    {
        foreach ($datainterval as $row) {
            if ($row['roomid'] == $roomId) {
                return true;
            }
        }

        return false;
    }

    function register_room($data)
    {
        $this->load->database();

        $this->db->query('insert into rezerved values ( 0,' .
                                                                 $data['roomid'] . ',' .
                                                                 $data['userid'] . ',' .
                                                                 $data['startdate'] . ',' .
                                                                 $data['enddate'] . ',' .
                                                                 $data['pool'] . ',' .
                                                                 $data['jacuzzi'] . ',' .
                                                                 $data['massage'] . ',' .
                                                                 $data['haman'] . ',' .
                                                                 $data['sauna'] . ',' .
                                                                 $data['salt'] . ',' .
                                                                 $data['fitness'] . ',' .
                                                                 $data['steam_bath'] . ');');
    }
}